package com.kc.web.controller.business;

import com.kc.business.domain.BizScenicSite;
import com.kc.business.service.IBizScenicSiteService;
import com.kc.business.service.IBizSiteVideoService;
import com.kc.common.annotation.Log;
import com.kc.common.annotation.RepeatSubmit;
import com.kc.common.core.controller.BaseController;
import com.kc.common.core.domain.AjaxResult;
import com.kc.common.core.page.TableDataInfo;
import com.kc.common.enums.BusinessType;
import com.kc.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * TODO
 *
 * @author zhangshuo
 * @version 1.0
 * @date 2020/9/8
 */
@RestController
@RequestMapping("/business/site")
public class BizScenicSiteController extends BaseController {

    @Autowired
    private IBizScenicSiteService bizScenicSiteService;
    @Resource
    private IBizSiteVideoService siteVideoService;

    /**
     * 景点列表
     *
     * @param bizScenicSite
     * @return
     */
    @PreAuthorize("@ss.hasPermi('business:site:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizScenicSite bizScenicSite) {
        startPage();
        if (ObjectUtils.isEmpty(bizScenicSite.getIsDeleted())) {
            bizScenicSite.setIsDeleted(0);
        }
        List<BizScenicSite> list = bizScenicSiteService.getList(bizScenicSite);
        for (BizScenicSite site : list) {
            if (StringUtils.isNotBlank(site.getType())) {
                //字符串转景点类型数组
                site.setTypes(StringUtils.split(site.getType(), ","));
            }

        }
        return getDataTable(list);
    }

    /**
     * 新增景点
     *
     * @param bizScenicSite
     * @return
     */
    @PreAuthorize("@ss.hasPermi('business:site:add')")
    @Log(title = "景点管理", businessType = BusinessType.INSERT)
    @PostMapping
    @RepeatSubmit
    public AjaxResult add(@Validated @RequestBody BizScenicSite bizScenicSite) {
        //景点分类数组转字符串逗号分隔
        bizScenicSite.setType(StringUtils.join(bizScenicSite.getTypes(), ","));
        bizScenicSiteService.add(bizScenicSite);
        siteVideoService.setSiteVideo(bizScenicSite);
        return toAjax(1);
    }

    /**
     * 修改景点
     *
     * @param bizScenicSite
     * @return
     */
    @PreAuthorize("@ss.hasPermi('business:site:edit')")
    @Log(title = "景点管理", businessType = BusinessType.UPDATE)
    @PutMapping
    @RepeatSubmit
    public AjaxResult edit(@Validated @RequestBody BizScenicSite bizScenicSite) {
        //景点分类数组转字符串逗号分隔
        bizScenicSite.setType(StringUtils.join(bizScenicSite.getTypes(), ","));
        siteVideoService.setSiteVideo(bizScenicSite);
        return toAjax(bizScenicSiteService.edit(bizScenicSite));
    }

    /**
     * 删除景点
     *
     * @param siteId
     * @return
     */
    @PreAuthorize("@ss.hasPermi('business:site:remove')")
    @Log(title = "景点管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{siteId}")
    public AjaxResult remove(@PathVariable Integer siteId) {
        BizScenicSite bizScenicSite = new BizScenicSite();
        bizScenicSite.setId(siteId);
        bizScenicSite.setIsDeleted(1);
        return toAjax(bizScenicSiteService.edit(bizScenicSite));
    }
}
